package com.example.shishaolong.warehousemanager.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.shishaolong.warehousemanager.common.DateUtil;

public class WarehouseDBHelper extends SQLiteOpenHelper {

    //创建管理员信息表
    private static final String CREATE_ADMIN = "CREATE TABLE admin_info(" +
            "id integer PRIMARY KEY autoincrement," +
            "name VARCHAR(10)," +
            "password VARCHAR(32)," +
            "sex integer," +
            "level integer," +
            "add_date text," +
            "modify_date text)";

    //创建货物信息表
    private static final String CREATE_GOODS = "CREATE TABLE goods_info(" +
            "id integer PRIMARY KEY autoincrement," +
            "name VARCHAR(12) NOT NULL," +
//            "all_num integer NOT NULL," +
//            "current_num integer," +
            "receive_person VARCHAR(12)," +         //接收人员
            "describe VARCHAR(50)," +       //描述
            "buyer_name VARCHAR(50)," +       //购买者姓名
            "storage_date text," +           //入库日期
            "out_date text,"+              //出库日期
            "modify_date text"+            //修改日期
            ")";


    private static final String ADD_COLUMN_ANSWER = "alter table question add column "
            + "answer varchar(1)";

    public WarehouseDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //当第一次创建数据库时执行
        sqLiteDatabase.execSQL(CREATE_ADMIN);
        sqLiteDatabase.execSQL(CREATE_GOODS);
        //插入超级管理员记录
        String insertSuperAdmin1 = "insert into admin_info(name,password,sex,level,add_date,modify_date) " +
                "values('admin','admin',1,1,'"+DateUtil.getCurrentDateStr() +"','"+DateUtil.getCurrentDateStr()+"')";
        String insertSuperAdmin2 = "insert into admin_info(name,password,sex,level,add_date,modify_date) " +
                "values('时少龙','888888',1,1,'"+DateUtil.getCurrentDateStr() +"','"+DateUtil.getCurrentDateStr()+"')";
        sqLiteDatabase.execSQL(insertSuperAdmin1);
        sqLiteDatabase.execSQL(insertSuperAdmin2);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        //当在创建数据库后更新数据库结构时执行
        /*switch (oldVersion) {
            case 1:
                sqLiteDatabase.execSQL(CREATE_QUESTION);
            case 2:
                db.execSQL(ADD_COLUMN_ANSWER);
            default:
        }*/
    }
}
